import 'package:bxultimate/app/pages/application/organization/organization_announcement_list/widget/item.dart';
import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/widgets/keep_alive.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class OrganizationAnnouncementListPage extends StatefulWidget {
  const OrganizationAnnouncementListPage({Key? key}) : super(key: key);

  @override
  State<OrganizationAnnouncementListPage> createState() =>
      _OrganizationAnnouncementListPageState();
}

class _OrganizationAnnouncementListPageState
    extends State<OrganizationAnnouncementListPage> {
  final controller = Get.find<OrganizationAnnouncementListController>();
  final state = Get.find<OrganizationAnnouncementListController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: TDNavBar(
          backgroundColor: Colors.transparent,
          title: "公告",
          rightBarItems: [
            TDNavBarItem(
              iconWidget: Icon(Icons.add),
              action: () async{
                await Get.toNamed(RouterName.organizationAnnouncementPublish,
                    arguments: {
                      "id": controller.id,
                      "sysType": controller.sysType
                    });
              },
            ),
          ],
        ),
        body: _buildBody(),
      ),
    );
  }

  _buildBody() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TDTabBar(
          tabs: [
            for (var item in state.tabs)
              TDTab(
                text: item.label,
              )
          ],
          controller: controller.tabController,
          backgroundColor: Colors.transparent,
          showIndicator: true,
        ),
        Gaps.vGap8,
        Expanded(
            child: TabBarView(
          controller: controller.tabController,
          children: [
            for (var i = 0; i < state.tabs.length; i++)
              _buildPage(state.tabs[i].value, i)
          ],
        ))
      ],
    );
  }

  _buildPage(status, index) {
    return KeepAliveWrapper(
      child: AnnouncementItemCard(status: status, orgId: controller.id),
    );
  }

  @override
  void dispose() {
    Get.delete<OrganizationAnnouncementListController>();
    super.dispose();
  }
}
